﻿@using System.Reflection
@using XCode
@using XCode.Configuration
@{
    Layout = null;

    var role = Model.Role as Role;
    var menus = Menu.Root.AllChilds;
    //var pfs = EnumHelper.GetDescriptions<PermissionFlags>().Where(e => e.Key > PermissionFlags.None);
}
<script type="text/javascript">
    $(function () {
        // 授权列
        $('input.authorize').on('change', function () {
            var $this = $(this);
            var status = $this.prop('checked');
            var childkey = $this.attr('child');
            // 规避change 需要在失去焦点时触发的问题，设置值完成后手工再次触发该事件
            $('input[parentkey="' + childkey + '"]').prop('checked', status).change();
        });
        // 授权子项列
        $('input.pchild').on('change', function () {
            var $this = $(this);
            var status = $this.prop('checked');
            var childkey = $this.attr('pchild');

            $('table input[childkey="' + childkey + '"]').prop('checked', status).change();
        });
    });
</script>
<div class="form-group col-md-12">
    <label class="control-label col-md-2">授权</label>
    <div class="input-group col-md-10">
        <table class="table table-bordered table-hover table-striped table-condensed">
            <thead>
                <tr>
                    <th>名称</th>
                    <th>显示名</th>
                    <th>授权</th>
                    <th>操作权限</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var entity in menus)
                {
                    <tr>
                        <td>@entity.TreeNodeName</td>
                        <td>@entity.DisplayName</td>
                        <td>@Html.CheckBox("p" + entity.ID, role.Has(entity.ID), new { @class = "authorize", @child = "auth_child" + entity.ID, @parentkey = "auth_child" + entity.ParentID })</td>
                        <td>
                            @if (entity.Childs.Count == 0)
                            {
                                foreach (var item in entity.Permissions.OrderBy(e => e.Key))
                                {
                                    var id = "pf" + entity.ID + "_" + ((Int32)item.Key);
                                    @Html.CheckBox(id, role.Has(entity.ID, (PermissionFlags)item.Key), new { @parentkey = "auth_child" + entity.ID, @childkey = item.Value })
                                    @Html.Label(id, item.Value)
                                    <text>&nbsp;</text>
                                }
                            }
                            else
                            {
                                var slist = new List<String>();
                                foreach (var item in menus)
                                {
                                    slist = slist.Union(item.Permissions.Values).ToList();
                                }
                                // 使用 变量i 设置id 避免ID异常无法正常关联checkbox 和label
                                var i = 0;
                                foreach (var item in slist)
                                {
                                    var id = "pc_" + i;
                                    @Html.CheckBox(id, false, new { @class = "pchild", @pchild = item, @parentkey = "auth_child" + entity.ID })
                                    @Html.Label(id, item)
                                    <text>&nbsp;</text>
                                    i++;
                                }
                            }
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
</div>